/*
 * Forward the HTTPS request from libcurl to instance vm.
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#ifndef ENCLAVE_PROXY_H
#define ENCLAVE_PROXY_H

#include <pthread.h>

/* 1ms */
#define RECV_DATA_TIMEOUT_US    1000

/* 100ms */
#define CONNECT_TIMEOUT_US      (100 * 1000)

/* 5s */
#define VSOCK_TIMEOUT           5

#define RETRY_MAX_TIMES         5

/* default path for proxy */
#define CURL_SOCKET_PATH "/var/run/enclave_proxy.socket"

enum px_error_code {
    PX_NO_ERROR = 0,
    PX_INVALID_ARGUMENT = 1,
    PX_SOCKET_ERROR = 2,
    PX_BIND_ERROR = 3,
    PX_LISTEN_ERROR = 4,
    PX_CONNECT_ERROR = 5,
    PX_PTHREAD_ERROR = 6,
    PX_INTER_ERROR = 7,
    PX_NO_DATA = 8,
    PX_NO_CONNECT = 9,
};
typedef enum px_error_code px_error_code;

typedef struct un_sockpair_info {
    int server_sockfd;
    int client_sockfd;
} un_sockpair_info;

typedef struct vsockpair_info {
    int client_sockfd;
} vsockpair_info;

typedef struct connect_info {
    struct un_sockpair_info unix_info;
    struct vsockpair_info vsock_info;
    pthread_t thread_id;
} connect_info;

/*
 * setup_proxy() - Establish a channel for forwarding HTTPS requests.
 * @conn (input): Internal structure used to store informations.
 * @vsock_cid (input): cid of the parent vm.
 * @vsock_port (input): port of the parent vm.
 *
 * Context: Process context.
 *
 * Returns: The status of the operation.
 */
int setup_proxy(struct connect_info *conn, const int vsock_cid,
    const int vsock_port);

/*
 * close_proxy() - Close the channel established by @setup_proxy.
 * @conn (input): Internal structure used to store informations.
 *
 * Context: Process context.
 *
 * Returns: Void.
 */
void close_proxy(struct connect_info *conn);

#endif
